package com.woniuxy.joylife.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.constant.ManagerConstant;
import com.woniuxy.joylife.manager.VO.OwnerRoomVO;
import com.woniuxy.joylife.manager.VO.OwnerVO;
import com.woniuxy.joylife.manager.dao.*;
import com.woniuxy.joylife.manager.entity.*;
import com.woniuxy.joylife.manager.service.OwnerRoomService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("ownerRoomService")
public class OwnerRoomServiceImpl extends ServiceImpl<OwnerRoomDao, OwnerRoomEntity> implements OwnerRoomService {

    @Autowired
    OwnerDao ownerDao;

    @Autowired
    BlockDao blockDao;

    @Autowired
    UnitDao unitDao;

    @Autowired
    RoomDao roomDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OwnerRoomEntity> page = this.page(
                new Query<OwnerRoomEntity>().getPage(params),
                new QueryWrapper<OwnerRoomEntity>().eq("state",ManagerConstant.UNDELETED)
        );
        List<OwnerRoomEntity> ownerRoomEntities = page.getRecords();
        List<OwnerRoomVO> ownerRoomVOS = new ArrayList<>();
        for (OwnerRoomEntity e :
                ownerRoomEntities) {
            OwnerEntity ownerEntity = ownerDao.selectById(e.getOwnerId());
            OwnerRoomVO ownerRoomVO = new OwnerRoomVO();
            ownerRoomVO.setOrId(e.getOrId());
            ownerRoomVO.setOwnerName(ownerEntity.getName());
            BlockEntity blockEntity = blockDao.selectById(e.getBlocId());
            UnitEntity unitEntity = unitDao.selectById(e.getUniId());
            RoomEntity roomEntity = roomDao.selectById(e.getRoomId());
            ownerRoomVO.setBlockName(blockEntity.getName());
            ownerRoomVO.setUnitName(unitEntity.getName());
            ownerRoomVO.setRoomName(roomEntity.getName());
            ownerRoomVOS.add(ownerRoomVO);
        }
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(ownerRoomVOS);

        return pageUtils;
    }

    @Override
    public Boolean updateByAddress(OwnerVO ownerVO) {

        // 房间有人居住
        QueryWrapper<OwnerRoomEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bloc_id",ownerVO.getOwnerId())
                .eq("uni_id",ownerVO.getUnitId())
                .eq("room_id",ownerVO.getRoomId())
                .eq("state",ManagerConstant.UNDELETED);
        OwnerRoomEntity ownerRoomEntity = baseMapper.selectOne(queryWrapper);
        if(ownerRoomEntity==null){
            return false;
        }else{
            ownerRoomEntity.setBlocId(ownerVO.getBlockId());
            ownerRoomEntity.setUniId(ownerVO.getUnitId());
            ownerRoomEntity.setRoomId(ownerVO.getRoomId());
            ownerRoomEntity.setUpdatetime(new Date());
            baseMapper.updateById(ownerRoomEntity);
            return true;
        }


    }

}